// pages/login/login.js
import { request } from '../../request/index'
const md5 = require('../../crypto/md5.min.js')
var timer1 = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    eyeFlag: true,
    eyeFlag2: true,
    getCodeFlag: false,
    codeTimer: 60,
    form: {
      password: '',
      phone: '',
      code: '',
      confirmPassword: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var name = options.name
    console.log(options);
    this.setData({
      name
    })
    wx.setNavigationBarTitle({
      title: "注册/忘记密码"
    });
  },
  handleEyePassword() {
    this.setData({
      eyeFlag: !this.data.eyeFlag
    })
  },
  handleEyePassword2() {
    this.setData({
      eyeFlag2: !this.data.eyeFlag2
    })
  },
  tapRegister() {
    wx.navigateTo({
      url: '/pages/register/authcode',
      data: this.data.phone
    })
  },

  checkPhone(phone) {
    let str = /^1[3456789]\d{9}$/
    if (str.test(phone)) {
      return true
    } else {
      return false
    }
  },
  checkCode(code) {
    let str = /^\d{4}$/
    if (str.test(code)) {
      return true
    } else {
      return false
    }
  },
  checkPassword(password) {
    let str = /^(?!bai[0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/
    if (str.test(password)) {
      return true
    } else {
      return false
    }
  },
  InputPhone(e) {
    var name = e.currentTarget.dataset.name
    var form = this.data.form
    form[name] = e.detail.value
    this.setData({
      form
    })
  },
  register() {
    var phone = this.data.form.phone
    var code = this.data.form.code
    var password = this.data.form.password
    var confirmPassword = this.data.form.confirmPassword
    if (!this.checkPhone(phone)) {
      return this.showToast('手机号码有误')
    } else if (!this.checkCode(code)) {
      return this.showToast('验证码格式有误')
    } else if (!this.checkPassword(password)) {
      return this.showToast('密码格式应由6-20字母和数字组成')
    } else if (confirmPassword !== password) {
      return this.showToast('两次密码不一致')
    } else {
      var data = {
        phone: phone,
        authcode: parseInt(code),
        password: md5(password)
      }
      request({
        url: '/v1/register',
        method: 'post',
        data
      }).then(res => {
        if (res.data.errCode == 0) {
          this.showToast('注册成功')
          setTimeout(() => {
            wx.navigateTo({
              url:'/pages/login/login'
            })
           }, 500);
        } else {
          this.showToast(res.data.message)
        }
      })
    }
  },
  forget(){
    var phone = this.data.form.phone
    var code = this.data.form.code
    var password = this.data.form.password
    var confirmPassword = this.data.form.confirmPassword
    if (!this.checkPhone(phone)) {
      return this.showToast('手机号码有误')
    } else if (!this.checkCode(code)) {
      return this.showToast('验证码格式有误')
    } else if (!this.checkPassword(password)) {
      return this.showToast('密码格式应由6-20字母和数字组成')
    } else if (confirmPassword !== password) {
      return this.showToast('两次密码不一致')
    } else {
      var data = {
        phone: phone,
        authcode: parseInt(code),
        password: md5(password)
      }
      request({
        url: '/v1/modif/password',
        method: 'post',
        data
      }).then(res => {
        if (res.data.errCode == 0) {
          this.showToast('修改密码成功')
         setTimeout(() => {
          wx.navigateTo({
            url:'/pages/login/login'
          })
         }, 500);
        } else {
          this.showToast(res.data.message)
        }
      })
    }
  },
  getCode() {
    console.log(this.data.form.phone);
    if (this.checkPhone(this.data.form.phone)) {
      request({
        url: '/v1/authcode',
        method: 'post',
        data: {
          phone: parseInt(this.data.form.phone)
        }
      }).then(res => {        // 
        if (res.data.errCode == 0) {
          this.setData({
            getCodeFlag: true,
            codeTimer: 60
          })
          clearInterval(timer1)
          timer1 = setInterval(() => {
            if (this.data.codeTimer <= 0) {
              this.setData({
                getCodeFlag: false,
              })
            } else {
              this.setData({
                codeTimer: this.data.codeTimer - 1
              })
            }
          }, 1000);
        }else{
          this.showToast(res.data.message)
        }
      })

    } else {
      this.showToast('手机号码有误')
    }

  },
  showToast(title) {
    wx.showToast({
      title: title,
      duration: 1500,
      icon: 'none',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})